package com.xinghanlaser.applet.base.auth.controller;

import com.xinghanlaser.applet.base.auth.query.ResourceQuery;
import com.xinghanlaser.applet.base.auth.service.IResourceService;
import com.xinghanlaser.applet.base.auth.vo.*;
import com.xinghanlaser.applet.base.common.entity.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

import static com.xinghanlaser.applet.base.auth.constant.AdminConstant.DEFECT_PID_STR;

/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/4/30 10:21
 */
@RestController
@RequestMapping("/admin/resource")
@Api(value = "资源管理", tags = "资源管理")
public class ResourceController {
    @Resource
    private IResourceService service;

    @GetMapping("page")
    @ApiOperation("根据条件分页查询资源树")
    public ResponseEntity<ResponseEntity.PageResult<ResourceTreeVO>> pageByCondition(ResourceQuery query){
        return service.pageByCondition(query);
    }

    @GetMapping("tree")
    @ApiOperation("资源树查询")
    public ResponseEntity<List<ResourceTreeVO>> tree(@RequestParam(defaultValue = DEFECT_PID_STR, required = false) Long pid) {
        return ResponseEntity.ok(service.tree(pid));
    }

    @GetMapping("listByRoleId/{roleId}")
    @ApiOperation("根据角色ID查询资源")
    public ResponseEntity<Set<ResourceVO>> getResourceListByRoleId(@PathVariable("roleId") Long id){
        return service.getResourceListByRoleId(id);
    }
    @DeleteMapping
    @ApiOperation("根据资源ID递归删除资源，会连同选中资源的子资源一并删除，请谨慎")
    public ResponseEntity<Void> delete(@RequestParam @NotNull(message = "未选中资源删除") Long id) {
        return  service.removeResource(id);
    }

    @PostMapping
    @ApiOperation("新增资源")
    public ResponseEntity<Void> addResource(@RequestBody @Valid ResourcePostVO postVO){
        return service.addResource(postVO);
    }

    @PutMapping
    @ApiOperation("修改资源信息")
    public ResponseEntity<Void> modResource(@RequestBody @Valid ResourcePutVO putVO){
        return service.modResource(putVO);
    }
}
